package ssf.core.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * Service 基类 设置log及基本的数据库访问<BR>
 * 基于Struts 1.X
 * 
 * @author Nanlei
 */
public class CommonBaseService {
	protected final Log log = LogFactory.getLog(getClass());

	/* JdbcTemplate */
	protected JdbcTemplate jt;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/* NamedParameterJdbcTemplate */
	protected NamedParameterJdbcTemplate npjt;

	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.npjt = namedParameterJdbcTemplate;
	}

	/* CallableStatementTemplate */
	protected CallableStatementTemplate cst;

	public void setCallableStatementTemplate(
			CallableStatementTemplate callableStatementTemplate) {
		this.cst = callableStatementTemplate;
	}

	/* PagingList */
	public PagingList getPagingList(String sql, HttpServletRequest request) {
		int pageNum = 1;
		int pageSize = 10;
		if (request.getParameter("pageNum") != null) {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		return new PagingList(sql, null, pageNum, pageSize, jt, request);
	}

	public PagingList getPagingList(String sql, Object[] params,
			HttpServletRequest request) {
		int pageNum = 1;
		int pageSize = 10;
		if (request.getParameter("pageNum") != null) {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		return new PagingList(sql, params, pageNum, pageSize, jt, request);
	}

	public PagingList getPagingList(String sql, Map namedParams,
			HttpServletRequest request) {
		int pageNum = 1;
		int pageSize = 10;
		if (request.getParameter("pageNum") != null) {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		return new PagingList(sql, namedParams, pageNum, pageSize, npjt,
				request);
	}
}